<?PHP
if(!defined('URLFZ'))
	die('Hacking Attempt');

function verifyURL($url){
if(!preg_match('|^['.utf8_decode(ALLOWED_CHARS).']{1,12}$|', $url))
{
	die('That is not a valid short url');
}
return true;
}

function getIDFromShortenedURL ($string, $base = ALLOWED_CHARS)
{
	$base = utf8_decode($base);
	//length of allowed chars list
	$length = strlen($base);
	//length of string. 
	$size = strlen($string) - 1;
	//turn it into a char array
	$string = str_split($string);
	//
	$out = strpos($base, array_pop($string));
	foreach($string as $i => $char)
	{
		$out += strpos($base, $char) * pow($length, $size - $i);
	}
	return $out;
}

function useCache($id){
	if(is_file(CACHE_DIR . $id))
		$long_url = file_get_contents(CACHE_DIR . $id);
	if(empty($long_url) || !preg_match(PROTOCALS, $long_url))
	{
		$long_url = getLong($id);
		@mkdir(CACHE_DIR, 0777);
		$handle = fopen(CACHE_DIR . $id, 'w+');
		fwrite($handle, $long_url);
		fclose($handle);
	}
	return $long_url;
}

function writeHeaders($location, $code = 301){
//if($code == 301)
//header('HTTP/1.1 301 Moved Permanently');
//else
header('HTTP/1.1 307 Temporary Redirect');
header('Location: ' .  $location);
exit;
}
?>
